global class PlanEligibility
{
    Webservice static String[] getUserTitles()
    {
        String[] titles = new List<String>();
        Map<String,String> checkTitle = new Map<String, String>();

        try
        {
            for(User[] users: [select Title from User where Title != '' order by Title])
            {
                for(User u:users)
                {
                    if(checkTitle.get(u.Title) == null)
                    {
                        checkTitle.put(u.Title, u.Title);
                        titles.add(u.Title);

                    }
                }
            }
        }
        catch(Exception e)
        {
            //return e.getMessage();
        }
        
        return titles;
    }//end of getUserTitles()
    
    WebService static String insertRecord(String roleTitle, String planId, String eligibilityType)
    {
        Plan_Role__c[] planRole = null;
        
        if(eligibilityType == 'role')
            planRole = [SELECT RoleID__c, Plan__c  from Plan_Role__c p where p.RoleID__c= :roleTitle and p.Plan__c = :planId];
        else
            planRole = [SELECT RoleID__c, Plan__c  from Plan_Role__c p where p.RoleTitle__c= :roleTitle and p.Plan__c = :planId];   
       
        if(planRole.size() > 0)
        {
            return 'Record already exists';
        }
     
        Plan__c[] plan = [select Id, Type__c from Plan__c where Id = :planId];
        if(plan.size() > 0)
        {
            if(eligibilityType == 'role')
                plan[0].Type__c = 'Role';
            else
                plan[0].Type__c = 'Title';
            
        }
        try
        {
            update plan;
        }
        catch(Exception e){}
        
        Plan_Role__c newPlanRole = null;
        if(eligibilityType == 'role')
        {
            //Query to find Roll Names and Description.
            UserRole[] ur = [select Name,RollupDescription from UserRole where Id = :roleTitle];
            if(ur.size() > 0)
            {
                 newPlanRole = new Plan_Role__c(RoleID__c = roleTitle, plan__c = planId, RoleTitle__c = ur[0].Name, Description__c = ur[0].RollupDescription);
                try
                {
                    insert newPlanRole;
                }
                catch(DmlException e)
                {}
            }
        }
        else// if eligibility type is 'title'
        {
            newPlanRole = new Plan_Role__c(plan__c = planId, RoleTitle__c = roleTitle);
            try
            {
                insert newPlanRole;
            }
            catch(DmlException e){}
        }
        
        return 'Record saved successfully.';
    }
    
    //Delete eligibility selected from the Plan Role object.
    WebService static String deleteEligibility(String planId, String planRoleId)
    {
        Plan_Role__c[] planRole = [SELECT Id from Plan_Role__c p where p.Id= :planRoleId and p.Plan__c = :planId];  
        
        if(planRole.size() > 0)
        {
            if(planRole[0]!=null)
                delete planRole[0];
        }
        return 'Deleted Successfully';
    }
    
    /**
     *  Invoked, if all Eligibility Types are deleted for a Plan in PlanRole object.
     *  Enables to select either of the Eligibility Types (By Role or By Title).
    */
    Webservice static String updatePlanType(String planId)
    {
        Plan__c[] plans = [select Type__c from Plan__c where id = :planId];
        
        if(plans.size() == 0)
            return null;

        plans[0].Type__c = '';
        update plans;
        return 'success';
    }
}